package com.nangle.managehost.manager;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.nangle.managehost.db.DBManager;
import com.nangle.managehost.db.SQLiteTemplate;
import com.nangle.managehost.entity.Bed;
import com.nangle.managehost.entity.BedMessage;


import java.util.List;

/**
 * Created by YFB on 2017/4/13.
 */

public class BedManger {
    private static BedManger bedManger = null;
    private static DBManager manager = null;
    private BedManger(Context context) {
        manager = DBManager.getInstance(context,  "yjhcloud");
    }
    public static BedManger getInstance(Context context) {

        if (bedManger == null) {
            bedManger = new BedManger(context);
        }

        return bedManger;
    }
    public long saveBed(Bed patient){
        SQLiteTemplate st = SQLiteTemplate.getInstance(manager, false);
        ContentValues contentValues = new ContentValues();

        contentValues.put("bednumber",patient.getBedNumber());
        contentValues.put("roomnumber", patient.getRoomNumber());
        contentValues.put("nurse_name",patient.getNurse_name());
        contentValues.put("type",patient.getType());
        return st.insert("bed", contentValues);
    }
    public List<Bed> getBedList(){
        SQLiteTemplate st = SQLiteTemplate.getInstance(manager, false);
        List<Bed> list = st.queryForList(new SQLiteTemplate.RowMapper<Bed>() {

            @Override
            public Bed mapRow(Cursor cursor, int index) {
                Bed notice = new Bed();
                notice.setId(cursor.getString(cursor.getColumnIndex("_id")));
                notice.setBedNumber(cursor.getInt(cursor
                        .getColumnIndex("bednumber")));
                notice.setRoomNumber(cursor.getInt(cursor.getColumnIndex("roomnumber")));
                notice.setType(cursor.getInt(cursor.getColumnIndex("type")));
                notice.setNurse_name(cursor.getString(cursor.getColumnIndex("nurse_name")));

                return notice;
            }

        }, "select * from bed" , null);
        return list;
    }
    public List<Bed> getBedList(String roomnumber){
        SQLiteTemplate st = SQLiteTemplate.getInstance(manager, false);
        List<Bed> list = st.queryForList(new SQLiteTemplate.RowMapper<Bed>() {

            @Override
            public Bed mapRow(Cursor cursor, int index) {
                Bed notice = new Bed();
                notice.setId(cursor.getString(cursor.getColumnIndex("_id")));
                notice.setBedNumber(cursor.getInt(cursor
                        .getColumnIndex("bednumber")));
                notice.setRoomNumber(cursor.getInt(cursor.getColumnIndex("roomnumber")));
                notice.setType(cursor.getInt(cursor.getColumnIndex("type")));
                notice.setNurse_name(cursor.getString(cursor.getColumnIndex("nurse_name")));

                return notice;
            }

        }, "select * from bed where roomnumber=?" , new String[] { roomnumber });
        return list;
    }
    public Bed getDoctor(String id){
        SQLiteTemplate st = SQLiteTemplate.getInstance(manager, false);
        return st.queryForObject(new SQLiteTemplate.RowMapper<Bed>() {

            @Override
            public Bed mapRow(Cursor cursor, int index) {
                Bed notice = new Bed();
                notice.setId(cursor.getString(cursor.getColumnIndex("_id")));
                notice.setBedNumber(cursor.getInt(cursor
                        .getColumnIndex("bednumber")));
                notice.setRoomNumber(cursor.getInt(cursor.getColumnIndex("roomnumber")));
                notice.setType(cursor.getInt(cursor.getColumnIndex("type")));
                notice.setNurse_name(cursor.getString(cursor.getColumnIndex("nurse_name")));


                return notice;
            }

        }, "select * from bed where _id=?", new String[] { id });
    }
}
